//
//  WSBaseViewController.m
//  WSBaseKit
//
//  Created by zhangcong on 2020/7/17.
//  Copyright © 2020 zhangcong. All rights reserved.
//

#import "WSBaseViewController.h"
#import <UIViewController+WSCategory.h>

@interface WSBaseViewController ()

@end

@implementation WSBaseViewController


- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self setUpBase];
    };
    
    return self;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setUpBase];
    }
    
    return self;
}

- (void)setUpBase{
    _hasBackItem = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [UIViewController attemptRotationToDeviceOrientation];
    
        
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])        // 解决vc偏移64的问题
        self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setUpBackItem];
}

- (void)setUpBackItem{
    if (self.hasBackItem) {
        [self loadLeftItemWithImage:@"icon_back" action:@selector(backAction)];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    if(!self.forbidAutoControlNavgion)
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    //遍历找到navigation下面的那条线
    for (UIView * view in self.navigationController.navigationBar.subviews[0].subviews) {
        if (view.frame.size.height < 1.1) {
            view.hidden = [self hideNavLine];
            break;
        }
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)backAction {
    NSArray *viewcontrollers=self.navigationController.viewControllers;
    if (viewcontrollers.count>1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count-1]==self) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else{
        [self dismissViewControllerAnimated:YES completion:nil];
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationFade;
}

- (void)dealloc {
    NSLog(@"dealloc %@",NSStringFromClass(self.class));
    //    if([[AppTool shared] usedMemory])
    //        [[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];
}


@end
